const express=require('express');
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');
const useServer=require('../service/userService');

var router=express.Router();

router.use('/loginPage.html',async  function (req,res,next) {
    var user=req.cookies['user'];
    if(user=={}||user==undefined){
        //1.loginPage.html
        next();
    }else{
        //res.redirect()
       var data=await useServer.logincookie(user);
        if(data.success){
            req.session['user']=data.user;
            //客户端发出的信息
            res.redirect('/views/pages/user1.html');
            //2.user1.html
            next();
        }else{
            //3.loginPage.html
            next();
        }
    }
});

//进行pages页面，必须是已登录状态，否则全部进入/views/loginPage.html
router.use('/pages',(req,res,next)=>{
    var user=req.session['user'];
    if(user==null||user=={}||user==undefined){
     res.redirect('views/loginPage.html');
    }else{
        next();
    }
})
exports.router=router;